package com.hqyj.product.controller;

import com.hqyj.product.model.MaterialsInfo;
import frame.product.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/materialsController")
public class MaterialsController extends BaseController {
    @RequestMapping("/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, MaterialsInfo materialsInfo) {
        //1.接收数据;
//        System.out.println("-----------list-------");
        //3.调用业务;
        List<MaterialsInfo> materialsInfo_dbList = materialsInfoService.queryAllMaterialsInfo();
        System.out.println(materialsInfo_dbList);
        model.addAttribute("materialsInfo_dbList", materialsInfo_dbList);
        //2.跳转页面;
        return "view/product/materialsInfo/materialsInfoList";
    }

    @RequestMapping("/add.do")
    public String add(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, MaterialsInfo materialsInfo) {
        //1.接收数据;
//        System.out.println("-----------add------" + materialsInfo);
        //3.调用业务;
        int n = materialsInfoService.addMaterialsInfoByMaterialsInfo(materialsInfo);

        //2.跳转页面;
        List<MaterialsInfo> materialsInfo_dbList = materialsInfoService.queryAllMaterialsInfo();
        model.addAttribute("materialsInfo_dbList", materialsInfo_dbList);
        return "/view/product/materialsInfo/materialsInfoList";
    }

    @RequestMapping("/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, MaterialsInfo materialsInfo, String idStr) {
        //1.接收数据;
        System.out.println("-----------delete------" + idStr);
        //3.调用业务;
        //字符类型转数字类型
        int id = Integer.parseInt(idStr);
        //业务
        int n = materialsInfoService.deleteMaterialsInfoById(id);

        //2.跳转页面;
        List<MaterialsInfo> materialsInfo_dbList = materialsInfoService.queryAllMaterialsInfo();
        model.addAttribute("materialsInfo_dbList", materialsInfo_dbList);
        return "view/product/materialsInfo/materialsInfoList";
    }

    @RequestMapping("/updateUI.do")
    public String updateUI(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, MaterialsInfo materialsInfo, String idStr) {
        //字符类型转数字类型
        int materialsInfoId = Integer.parseInt(idStr);
        //业务
        MaterialsInfo materialsInfo_db = materialsInfoService.queryMaterialsInfoById(materialsInfoId);
        System.out.println("-----------updateUI------" + materialsInfo_db);
        model.addAttribute("materialsInfo", materialsInfo_db);
        //2.跳转页面;
        return "view/product/materialsInfo/updateMaterialsInfo";
    }

    @RequestMapping("/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, MaterialsInfo materialsInfo, String id) {
        //1.接收数据;
        //字符类型转数字类型
        int materialsInfoId = Integer.parseInt(id);

        materialsInfo.setProductId(materialsInfoId);
        System.out.println(materialsInfo);
        //3.调用业务;
        int n = materialsInfoService.updateMaterialsInfoByMaterialsInfo(materialsInfo);


        //2.跳转页面;
        List<MaterialsInfo> materialsInfo_dbList = materialsInfoService.queryAllMaterialsInfo();
        model.addAttribute("materialsInfo_dbList", materialsInfo_dbList);
        return "view/product/materialsInfo/materialsInfoList";
    }
}
